class SocketConnection {
	protected static _instance:SocketConnection;
	protected _ip:string;
	protected _port:number;
	protected _sendBeat:boolean=false;
	protected _timeKey :number ;
	protected _pingkey :number ;
	public static init(ip,port):void
	{
		if(SocketConnection._instance)
			return;
		SocketConnection._instance=new SocketConnection(ip,port);
	}
	public static get instance():SocketConnection
	{
		return SocketConnection._instance;
	}
	public constructor(ip,port) {
		this._timer=new egret.Timer(30000);
		this._timer.addEventListener(egret.TimerEvent.TIMER,this.onTimer,this);
		this._timer.start();
		this._ip=ip;
		this._port=port;
		//this.start(ip,port);
	}
	protected _socket:egret.WebSocket;
	protected _timer:egret.Timer;
	public start(ip?:string,port?:number):void{
		var self = this ;
		if(ip)this._ip=ip;
		if(port)this._port=port;
		if(this._socket)
		{
			this._socket.removeEventListener(egret.Event.CONNECT,this.onConnect,this);
			this._socket.removeEventListener(egret.ProgressEvent.SOCKET_DATA,this.onSocketData,this);
			this._socket.removeEventListener(egret.Event.CLOSE,this.onClose,this);
			this._socket.removeEventListener(egret.IOErrorEvent.IO_ERROR,this.onError,this);
		}
		//UIManager.instance.showLoading("正在连接");
		this._socket=new egret.WebSocket();
		this._socket.addEventListener(egret.Event.CONNECT,this.onConnect,this);
		this._socket.addEventListener(egret.ProgressEvent.SOCKET_DATA,this.onSocketData,this);
		this._socket.addEventListener(egret.Event.CLOSE,this.onClose,this);
		this._socket.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onError,this);
		this._socket.connectByUrl("ws://"+this._ip+":"+this._port+"/ws");
		this._timeKey = egret.setTimeout(function(){
			self.close();
			self.start();
		},this,10000);
	}
	public close()
	{
		this._socket.close();
	}
	public send(type,data:any):void{
		if(this._socket&&this._socket.connected)
		{
			data.sessionKey=NetGlobalData.token||"";
			data.fun=type;
			var str=JSON.stringify(data);
			this._socket.writeUTF(str);
			this._socket.flush();
			LogManager.log("request:"+str);
		}
	}

	public sendString(data)
	{
		if(this._socket&&this._socket.connected)
		{
			this._socket.writeUTF(data);
			this._socket.flush();
			LogManager.log("request:"+data);
		}
	}

	public setHeartBeat(value:boolean)
	{
		this._sendBeat=value;
		if(!value)	
			egret.clearTimeout(this._pingkey);
	}

	protected onConnect(e:egret.Event):void
	{
		//UIManager.instance.removeLoading();
		egret.clearTimeout(this._timeKey);
		NotificationManager.instance.sendMsg(NotificationConst.SOCKET_CONNECT);
		LogManager.log("socket connect");
	}
	protected onSocketData(e:egret.ProgressEvent)
	{
		var data=this._socket.readUTF();
		var obj=JSON.parse(data);
		if(obj.code==0)
		{
			//UIManager.instance.removeLoading();
			NotificationManager.instance.sendMsg(obj["fun"],data);
		}
		else
		{
			NotificationManager.instance.sendMsg(obj["fun"],data);
			if(obj["fun"]=="ping"){
				egret.clearTimeout(this._pingkey);
				this._pingkey = egret.setTimeout(function(){
					//UIManager.instance.showScene(GameLoginScene,GameLoginScene.loginViewState);
				},this,35000);
			}
		}
		LogManager.log("onSocketData"+data);
	}
	protected onClose(e:egret.Event)
	{
		NotificationManager.instance.sendMsg(NotificationConst.SOCKET_CLOSE);
		//this.start();
		//UIManager.instance.showScene(GameLoginScene,GameLoginScene.loginViewState);
		LogManager.log("socket closed");
	}
	protected onError(e:egret.Event)
	{
		//this.start();
		//UIManager.instance.showScene(GameLoginScene,GameLoginScene.loginViewState);
		LogManager.log("socket ioError");
	}

	//----------------------------------------------
	protected onTimer(e:egret.TimerEvent)
	{
		if(this._sendBeat&&this._socket&&this._socket.connected)
		{
			this.send("ping",{});
		}
	}
}